﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IObjects;
using Entities;
using System.Data;
using System.Data.SqlClient;
using DataObjects.EFData;
using Utilities;

namespace DataObjects
{
    public class Config :  IConfig
    {
        private ConfigInfo _configInfo;
        private string mode = string.Empty;
        private WebOnlineEntities datacontext;
        # region Implement Interface
        public ConfigInfo ConfigInfo
        {
            get
            {
                return _configInfo;
            }
            set
            {
                _configInfo = value;
            }
        }

        public string Mode
        {
            get
            {
                return mode;
            }
            set
            {
                mode = value;
            }
        }

        public int Save()
        {
            if (mode == Constants.MODE_INSERT)
            {
                return Insert();
            }
            if (mode == Constants.MODE_UPDATE)
            {
                return Update();
            }
            if (mode == Constants.MODE_INSERT)
            {
                return Delete();
            }
            throw new Exception("Mode is invalid in Config object");
        }

        public IList<ConfigInfo> GetConfigs()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var configList = datacontext.TB_Configuration.Select(c => c.ToConfigInfo()).ToList();
                return configList;
            }
        }

        public ConfigInfo GetById(string id)
        {
            using (datacontext = new WebOnlineEntities())
            {
                var GuidId=new Guid(id);
                var config = datacontext.TB_Configuration.Where(c => c.ConfigurationID == GuidId).FirstOrDefault();
                if (config!=null)
                {
                    return config.ToConfigInfo();
                }
                return null;
            }
        }

        public ConfigInfo GetByName(string name)
        {
            using (datacontext = new WebOnlineEntities())
            {
                var config = datacontext.TB_Configuration.Where(c => c.Name == name).FirstOrDefault();
                if (config != null)
                {
                    return config.ToConfigInfo();
                }
            }
            return null;
        }
        #endregion
        #region Private Methods
        private int Insert()
        {
            using (datacontext = new WebOnlineEntities())
            {
                datacontext.AddToTB_Configuration(this.ConfigInfo.ToTB_Configuration());
                return datacontext.SaveChanges();
            }
        }

        private int Update()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var config = datacontext.TB_Configuration.Where(c => c.ConfigurationID == _configInfo.ConfigurationId).FirstOrDefault();
                config.Name = _configInfo.Name;
                config.Value = _configInfo.Value;
                config.IsEncrypted = _configInfo.IsEncrypted;
                config.IsSystem = _configInfo.IsSystem;
                config.ModifiedBy = _configInfo.ModifiedBy;
                config.ModifyDate = _configInfo.ModifyDate;
                return datacontext.SaveChanges();
            }
        }

        private int Delete()
        {
            using (datacontext = new WebOnlineEntities())
            {
                datacontext.DeleteObject(datacontext.TB_Configuration.FirstOrDefault(c => c.ConfigurationID == _configInfo.ConfigurationId));
                return datacontext.SaveChanges();
            }
        }
        #endregion
    }
}
